package org.maxgroundstation.groundstation.view.overlay;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Iterator;

import javax.swing.JComponent;

import org.maxgroundstation.groundstation.GroundStation;
import org.maxgroundstation.groundstation.view.CameraPanel;

/**
 * A component for displaying camera overlay graphics.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@SuppressWarnings("serial")
public class CameraOverlayComponent extends JComponent implements Runnable {

    // Length of refresh thread sleep time (milliseconds).
    private static final long SLEEP_TIME = 100L;
    
    // Data members
    private CameraPanel cp;
    private Thread refreshThread;
    private boolean run;
    
    /**
     * Constructor
     * @param gs the ground station.
     * @param cp the camera panel.
     */
    public CameraOverlayComponent(GroundStation gs, CameraPanel cp) {
        super();
        
        // Initialize data members
        this.cp = cp;

        // Initialize overlays.
        OverlayUtil.addOverlay(new VideoImageDataOverlay(gs));
        OverlayUtil.addOverlay(new PingLatencyOverlay(gs));
        OverlayUtil.addOverlay(new GeometryOverlay(gs));
        OverlayUtil.addOverlay(new WiFiSignalStrengthOverlay(gs));
        OverlayUtil.addOverlay(new GpsOverlay(gs));
        
        // Start refresh thread.
        start();
    }
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g;
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        
        Iterator<Overlay> i = OverlayUtil.getOverlays().iterator();
        while (i.hasNext()) {
            Overlay overlay = i.next();
            if (overlay.canDisplay() && overlay.getDisplay()) {
                overlay.paintOverlay(g2d, getWidth(), getHeight());
            }
        }
    }

    /**
     * Start the refresh thread.
     */
    private void start() {
        if ((refreshThread == null) || !run) { 
            refreshThread = new Thread(this, "camera overlay refresh thread");
            refreshThread.start();
        }
    }
    
    @Override
    public void run() {
        run = true;
        while (run) {
            try {
                Thread.sleep(SLEEP_TIME);
            } 
            catch (InterruptedException e) {}
            
            // Repaint if haven't repainted since sleep.
            long repaintTime = System.currentTimeMillis() - SLEEP_TIME;
            if (cp.getLastTimePainted() < repaintTime) cp.repaint();
        }
    }
}